package com.glela.goods.service.impl;

import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.glela.cache.redis.RedisUtil;
import com.glela.goods.mapper.GoodsNosendRegionMapper;
import com.glela.goods.model.GoodsNosendRegion;
import com.glela.goods.service.GoodsNosendRegionService;
import com.glela.platform.model.UserAddress;
import com.glela.platform.service.UserAddressService;


@Service("goodsNosendRegionService")
@Scope("singleton")
public class GoodsNosendRegionServiceImpl implements GoodsNosendRegionService {
	
	@Autowired
	private GoodsNosendRegionMapper goodsNosendRegionMapper;
	
	@Autowired
	private RedisUtil redisUtil;
	
	public List<GoodsNosendRegion> getNoSendRegionByGoodsId(Long goodsId){
		return getNoSendRegionByGoodsId(goodsId,1);
	}
	
	public List<GoodsNosendRegion> getNoSendRegionByGoodsId(Long goodsId , Integer level){
		return goodsNosendRegionMapper.getNoSendRegionByGoodsId(goodsId,level);
	}
	
	public boolean exist(Long goodsId,String regionName,Integer level) {
		String key = "p:nosend:adr:" + goodsId;
		if(!redisUtil.exists(key)) {
			//重新缓存不发货地区按省
			List<GoodsNosendRegion> _list = getNoSendRegionByGoodsId(goodsId);
			if(CollectionUtils.isNotEmpty(_list)) {
				for(GoodsNosendRegion gr : _list) {
					redisUtil.sadd(key, gr.getRegionName());
				}
			}
			if(CollectionUtils.isEmpty(_list)) {
				redisUtil.sadd(key, "IS NULL");
			}
		}
		return redisUtil.sismember(key, regionName);
	}
	
}
